import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'
import mitt from "mitt"



// 引入 vue-router
import { createRouter, createWebHistory } from 'vue-router'
// 引入 element-plus
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/theme-chalk/display.css'
// 暗黑模式
// import 'element-plus/theme-chalk/dark/css-vars.css'
// 引入axios
import axios from 'axios'



// 示例路由配置
import Home from './views/Home.vue'
import Login from './views/Login.vue'
import BalanceCheckComplete from './views/BalanceCheckComplete.vue'
import LiteratureReading from './views/LiteratureReading.vue'
import BatchReading from './views/BatchReading.vue'
import Record from './views/Record.vue'
import LiteratureDetail from './views/LiteratureDetail.vue'
import TranslationReading from './views/TranslationReading.vue'
import TranslationDetail from './views/TranslationDetail.vue'
import BatchDetail from './views/BatchDetail.vue'


const routes = [
  { path: '/', component: Home, name: 'Home', meta: {title: '首页', level: 1} },
  { path: '/home', component: Home, name: 'Home', meta: {title: '首页', level: 1} },
  { path: '/login', component: Login, name: 'Login', meta: {title: '登录页'} },
  { path: '/literatureReading', component: LiteratureReading, name: 'LiteratureReading', meta: {title: '文献阅读', level: 2} },
  { path: '/literatureDetail', component: LiteratureDetail, name: 'LiteratureDetail', meta: {title: '文献阅读', level: 3} },
  { path: '/translationReading', component: TranslationReading, name: 'TranslationReading', meta: {title: '文献翻译', level: 2} },
  { path: '/translationDetail', component: TranslationDetail, name: 'TranslationDetail', meta: {title: '文献翻译', level: 3} },
  { path: '/batchReading', component: BatchReading, name: 'BatchReading', meta: {title: '批量阅读', level: 2} },
  { path: '/batchDetail', component: BatchDetail, name: 'BatchDetail', meta: {title: '批量阅读', level: 3} },
  { path: '/record', component: Record, name: 'Record', meta: {title: '历史记录', level: 2} },
  { path: '/balanceCheckComplete', component: BalanceCheckComplete, name: 'BalanceCheckComplete', meta: {title: '余额查询'} },
  // 你可以在这里添加更多页面
  { path: '/:pathMatch(.*)*', redirect: '/home' }, // 未匹配到的路由重定向到home
]

const router = createRouter({
  history: createWebHistory('/aipaper/'), // 添加 base 配置，以匹配你的子路径部署
  routes
})

router.beforeEach((to,from,next)=>{
  // to: 从哪个页面
  // from: 到哪个页面
  // next: 只有执行next()页面
 
  //判断用户是否登录
  if(!localStorage.getItem('token')){
    // 未登录，跳转到login页面
    if( to.path === '/login'){
      next()
      return
    }
    next('/login')
  }else{
    // 已登录
    next()
  }
})


const app = createApp(App)
// 全局注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(router)
app.use(ElementPlus, {
  locale: zhCn
})
app.config.globalProperties.$bus = new mitt()
app.mount('#app')